//
//  FLMDropdownMenu.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/6.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMDropdownMenu.h"

@interface FLMDropdownMenuCell : UITableViewCell

@property (nonatomic, strong) UIView *lineView;

@end

@implementation FLMDropdownMenuCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.textLabel.textColor = [UIColor whiteColor];
        self.textLabel.font = [UIFont systemFontOfSize:13.0f];
        self.textLabel.width = self.width;
        
        [self.contentView addSubview:(_lineView = [UIView new])];
        _lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.width = 20;
    self.imageView.height = 20;
    self.imageView.centerY = self.height/2;
    self.imageView.left = 30;
    
    self.textLabel.width = self.width - self.imageView.right;
    self.textLabel.left = self.imageView.right + 20;
    self.textLabel.height = self.height;
    
    _lineView.top = self.height - 1;
    _lineView.height = 1;
    _lineView.width = self.width;
    _lineView.left = 0;
}

@end

CGFloat const kFLMDropdownMenuWidth = 150;
CGFloat kFLMDropdownMenuCellHeight = 44.0;

@interface FLMDropdownMenu ()<UITableViewDelegate,UITableViewDataSource>

/**
 *  显示具体内容的容器图片
 */
@property (nonatomic, weak) UIImageView *containerView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation FLMDropdownMenu
//懒加载
- (UIImageView *)containerView
{
    if (!_containerView) {
        UIImageView *containerView = [[UIImageView alloc] init];
        
        UIImage *image = [UIImage imageNamed:@"popover_background_left"];
        
        if(_type == FLMDropdownMenuRight) {
            image = [UIImage imageNamed:@"popover_background_right"];
        }
        else if(_type == FLMDropdownMenuMiddle){
            image = [UIImage imageNamed:@"popover_background"];
        }
        
        containerView.image = [self resizableImage:image];
        containerView.userInteractionEnabled = YES;
        
        [self addSubview:containerView];
        
        self.containerView = containerView;
    }
    return _containerView;
}

/**
 *  初始化对象
 *
 *  @param frame 指定尺寸
 *
 *  @return 返回一个新的视图对象,
 */
- (id)initWithFrame:(CGRect)frame type:(FLMDropdownMenuType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        //清除颜色
        self.backgroundColor = [UIColor clearColor];
        _type = type;
        
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kFLMDropdownMenuCellHeight;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[FLMDropdownMenuCell class] forCellReuseIdentifier:@"FLMDropdownMenuCell"];
        
        [self.containerView addSubview:_tableView];
    }
    return self;
}

- (UIImage *)resizableImage:(UIImage *)image
{
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height / 2 , image.size.width / 2 + 90, image.size.height / 2  , image.size.width / 2 ) resizingMode:UIImageResizingModeStretch];
}

/**
 *  下拉菜单方法
 *
 *  @return 返回下拉菜单安妮怒
 */
+ (instancetype)menu
{
    return [[self alloc] init];
}

+ (instancetype)menuWithType:(FLMDropdownMenuType)type
{
    return [[self alloc] initWithFrame:CGRectZero type:type];
}

- (NSString *)selectedTitle {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    return cell.textLabel.text;
}

/**
 *  显示
 */
- (void)showFrom:(UIView *)from
{
    //1.获取最上面的窗口
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    
    //2.添加自己到窗口中
    [window addSubview:self];
    
    //3.设置尺寸,系统自带的窗口创建出来就是全屏的
    self.frame = window.bounds;
    
    self.containerView.frame = CGRectMake(0, from.bottom, kFLMDropdownMenuWidth, 0);
    self.tableView.frame = CGRectMake(3, 10, self.containerView.width-6, 0);
    
    _tableView.height = _titles.count * kFLMDropdownMenuCellHeight;
    _containerView.height = _tableView.height + 10;
    
    //通知外界自己显示了
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
        [self.delegate dropdownMenuDidShow:self];
    }
}

- (void)showAtPoint:(CGPoint)start
{
    [self showFrom:nil];
    
    self.containerView.top = start.y;
    self.containerView.left = start.x;
}

/**
 *  隐藏
 */
- (void)dismiss
{
    // 通知外界自己被销毁了.
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)]) {
        [self.delegate dropdownMenuDidDismiss:self];
    }
    // 将菜单从窗口中移除
    [self removeFromSuperview];
}

//点击view调用方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //移除view
    [self dismiss];
}

#pragma mark - tableview
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FLMDropdownMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMDropdownMenuCell" forIndexPath:indexPath];
    cell.imageView.image = _images[indexPath.row];
//    if(_images.count) {
//        cell.imageView.image = _images[indexPath.row];
//    }
//    else {
//        cell.imageView.image = [UIImage imageNamed:@"9.9"];
//    }
    cell.textLabel.text  = _titles[indexPath.row];
    cell.lineView.hidden = indexPath.row == _titles.count - 1;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismiss];
}

@end
